package com.ruoyi.module.studyCenter.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.module.excellentCase.domain.ExcellentCase;
import com.ruoyi.module.excellentCaseDetail.domain.ExcellentCaseDetail;
import com.ruoyi.module.studyCenter.vo.StudyCenterVO;
import com.ruoyi.module.studyCenterDetail.domain.StudyCenterDetail;
import com.ruoyi.module.studyCenterDetail.mapper.StudyCenterDetailMapper;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.studyCenter.mapper.StudyCenterMapper;
import com.ruoyi.module.studyCenter.domain.StudyCenter;
import com.ruoyi.module.studyCenter.service.IStudyCenterService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 学习中心Service业务层处理
 * 
 * @author yqq
 * @date 2024-04-19
 */
@Service
public class StudyCenterServiceImpl implements IStudyCenterService 
{
    @Resource
    private StudyCenterMapper studyCenterMapper;

    @Resource
    private StudyCenterDetailMapper studyCenterDetailMapper;

    @Resource
    private ISysUserService userService;

    /**
     * 查询学习中心
     * 
     * @param studyId 学习中心主键
     * @return 学习中心
     */
    @Override
    public StudyCenter selectStudyCenterByStudyId(Long studyId)
    {
        return studyCenterMapper.selectStudyCenterByStudyId(studyId);
    }

    /**
     * 查询学习中心列表
     * 
     * @param studyCenter 学习中心
     * @return 学习中心
     */
    @Override
    public List<StudyCenter> selectStudyCenterList(StudyCenter studyCenter)
    {
        return studyCenterMapper.selectStudyCenterList(studyCenter);
    }

    /**
     * 新增学习中心
     * 
     * @param studyCenter 学习中心
     * @return 结果
     */
    @Override
    public int insertStudyCenter(StudyCenter studyCenter)
    {
        studyCenter.setCreateTime(DateUtils.getNowDate());
        return studyCenterMapper.insertStudyCenter(studyCenter);
    }

    /**
     * 修改学习中心
     * 
     * @param studyCenter 学习中心
     * @return 结果
     */
    @Override
    public int updateStudyCenter(StudyCenter studyCenter)
    {
        studyCenter.setUpdateTime(DateUtils.getNowDate());
        return studyCenterMapper.updateStudyCenter(studyCenter);
    }

    /**
     * 批量删除学习中心
     * 
     * @param studyIds 需要删除的学习中心主键
     * @return 结果
     */
    @Override
    public int deleteStudyCenterByStudyIds(Long[] studyIds)
    {
        return studyCenterMapper.deleteStudyCenterByStudyIds(studyIds);
    }

    /**
     * 删除学习中心信息
     * 
     * @param studyId 学习中心主键
     * @return 结果
     */
    @Override
    public int deleteStudyCenterByStudyId(Long studyId)
    {
        return studyCenterMapper.deleteStudyCenterByStudyId(studyId);
    }

    /**
     * 新增学习中心和详情
     * @param vo 新增参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertAllStudyCenter(StudyCenterVO vo) {
        StudyCenter studyCenter = new StudyCenter();
        BeanUtils.copyProperties(vo,studyCenter);
        studyCenter.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        studyCenter.setCreateTime(DateUtils.getNowDate());
        //获取作者信息
        String phone = SecurityUtils.getLoginUser().getUser().getPhonenumber();
        Map<String, String> authorInfo = userService.getAuthorInfo(phone);
        if (CollUtil.isNotEmpty(authorInfo)){
            studyCenter.setAuthorId(Long.valueOf(authorInfo.get("authorId")));
            studyCenter.setAuthorName(StringUtils.isNotBlank(authorInfo.get("memberNickname"))?authorInfo.get("memberNickname"):authorInfo.get("memberName"));
        }else {
            throw new BaseException(phone+"手机号注册会员");
        }
        studyCenterMapper.insertStudyCenter(studyCenter);
        List<StudyCenterDetail> studyCenterDetailList = vo.getCenterDetailList();
        if (CollUtil.isNotEmpty(studyCenterDetailList)){
            for (StudyCenterDetail studyCenterDetail : studyCenterDetailList) {
                studyCenterDetail.setsDetailId(studyCenter.getStudyId());
            }
            studyCenterDetailMapper.insertAllExcellentCaseDetail(studyCenterDetailList);
        }
    }


    /**
     * 修改学习中心
     * @param studyCenter
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAllStudyCenter(StudyCenterVO studyCenter) {
        StudyCenter vo = new StudyCenter();
        BeanUtils.copyProperties(studyCenter,vo);
        studyCenterMapper.updateStudyCenter(vo);
        //删除子集
        studyCenterDetailMapper.deleteStudyCenterDetailBysDetail(vo.getStudyId());

        List<StudyCenterDetail> studyCenterDetailList = studyCenter.getCenterDetailList();
        if (CollUtil.isNotEmpty(studyCenterDetailList)){
            for (StudyCenterDetail studyCenterDetail : studyCenterDetailList) {
                studyCenterDetail.setDetailId(null);
            }
            studyCenterDetailMapper.insertAllExcellentCaseDetail(studyCenterDetailList);
        }
    }

    /**
     *
     * @param studyId
     */
    @Override
    public void deleteAllStudyCenter(Long studyId) {
        studyCenterMapper.deleteStudyCenterByStudyId(studyId);
        studyCenterDetailMapper.deleteStudyCenterDetailBysDetail(studyId);
    }
}
